1 <?php
2
3 session_start();
4
5 include_once('includes/config.php');
6
7
8 #Auth Section
9 if (isset($_SESSION['email']) && isset($_SESSION['token'])) {
10
11 #Store retrieved session values
12 $email = $_SESSION['email'];
13 $token = $_SESSION['token'];
14
15 # if email and token is set check them against the database, retrieve and store the email and token retrieved for comparison
16
17 $sql = "SELECT user_email, user_token from users WHERE user_email = '$email'";
18 $retrieveStmt = $db_conn -> prepare($sql);
19 $retrieveStmt -> execute();
20
21 $user_row = $retrieveStmt -> fetch(PDO::FETCH_ASSOC);
22
23 if ($user_row > 0) {
24 # store values to be compared
25 $_server_email = $user_row['user_email'];
26 $_server_token = $user_row['user_token'];
27 }
28
29 }
30
31 if (isset($_SESSION['email']) && isset($_SESSION['token'])) {
32 if ($email == $_server_email && $token == $_server_token)
33 {
34 $arr = explode("@", $_SESSION['email'], 2);
35 $cartName = $arr[0] . '_cart';
36
37 $getCartProducts = $db_conn -> prepare("SELECT * FROM $cartName");
38 $getCartProducts -> execute();
39
40 $cartProducts = $getCartProducts -> fetchAll();
41 }
42 } else {
43 header('Location: index.php');
44 }
45
46
47 $totalCartProducts = 0;
48
49 if (isset($_SESSION['email'])) {
50 // Check if product is in user cart
51 $arr = explode("@", $_SESSION['email'], 2);
52 $cartName = $arr[0] . '_cart';
53
54 $allUserCartProducts = $db_conn -> prepare("SELECT * FROM $cartName");
55 $allUserCartProducts -> execute();
56
57 $totalCartProducts = $allUserCartProducts -> rowCount();
58
59 #Total Quantity of products
60 $stmtTotalProductQuantity = $db_conn -> prepare("SELECT SUM(product_quantity) FROM $cartName");
61 $stmtTotalProductQuantity -> execute();
62
63 $stmtTotalProductQuantityRow = $stmtTotalProductQuantity -> fetch(PDO::FETCH_NUM);
64
65 $totalProducts = $stmtTotalProductQuantityRow[0];
66
67 #Total Price of Products
68 $totalPriceProducts = $db_conn -> prepare("SELECT SUM(product_total) FROM $cartName");
69
70 $totalPriceProducts -> execute();
71
72 $totalPriceRow = $totalPriceProducts -> fetch(PDO::FETCH_NUM);
73
74 $totalPrice = $totalPriceRow[0];
75
76
77 # Get all products added to cart
78 $allCartProducts = $db_conn -> prepare("SELECT * FROM $cartName");
79 $allCartProducts -> execute();
80
81 $allCartProductsRow = $allCartProducts -> fetchAll();
82
83 }
84
85
86 ?>
87
88 <!DOCTYPE html>
89 <html>
90 <head>
91 <meta charset="utf-8" />
92 <meta http-equiv="X-UA-Compatible" content="IE=edge">
93 <title>Your Cart | MSwiss</title>
94 <meta name="viewport" content="width=device-width, initial-scale=1">
95
96 <!-- Favicons -->
97 <link rel="icon" type="image/png" href="images/icons/favicon-32x32.png" sizes="32x32" />
98 <link rel="icon" type="image/png" href="images/icons/favicon-128.png" sizes="128x128" />
99
100 <!-- Main CSS-->
101 <link rel="stylesheet" type="text/css" media="screen" href="css/main.css" />
102
103 <!-- Products CSS -->
104 <link rel="stylesheet" type="text/css" media="screen" href="css/products.css" />
105
106 <!-- Roboto font CDN -->
107 <link href="https://fonts.googleapis.com/css?family=Roboto:100,100i,300,300i,400,400i,500,500i,700,700i,900,900i" rel="stylesheet">
108
109 </head>
110 <body>
111
112 <div class="side-menu">
113 <ul>
114 <li>
115 <a href="index.php">
116 Home
117 </a>
118 </li>
119 <li>
120 <a href="products.php">
121 Shop
122 </a>
123 </li>
124 <li>
125 <a href="cart.php" class="active-link">
126 Cart
127 </a>
128 </li>
129 <li>
130 <a href="index.php">
131 About
132 </a>
133 </li>
134 <li>
135 <a href="index.php">
136 Contact
137 </a>
138 </li>
139 </ul>
140
141 <a href="#" class="disclaimer">Privacy Policy</a>
142 <a href="#" class="disclaimer">Disclaimer</a>
143 </div>
144
145 <div class="clearfix"></div>
146
147 <div class="overlay">
148
149 </div>
150
151 <div class="clearfix"></div>
152
153 <div class="login-wrapper">
154 <h3>Login</h3>
155 <form id="login-form">
156 <input type="email" id="login-email" name="login-email" placeholder="Email Address" required/>
157 <input type="password" id="login-password" name="login-password" placeholder="Password" required/>
158 <p></p>
159 <input id="login-btn" type="submit" value="Log in" />
160 </form>
161 </div>
162
163 <div class="signup-wrapper">
164 <h3>Sign up</h3>
165 <form id="signup-form">
166 <input type="text" id="signup-name" placeholder="Name*" required/>
167 <input type="email" id="signup-email" placeholder="Email Address*" required/>
168 <input type="password" id="signup-password" placeholder="Password*" required/>
169 <input type="text" id="signup-address" placeholder="Address*" required/>
170 <p></p>
171 <input id="signup-btn" type="submit" value="Sign up" />
172 </form>
173 </div>
174
175 <div class="container">
176
177 <nav>
178 <div class="menu-container">
179 <div class="menu-icon">
180 <span class="menu-aria"></span>
181 <span class="menu-aria"></span>
182 <span class="menu-aria"></span>
183 <div class="menu-text">
184 <p>Menu</p>
185 </div>
186 </div>
187 <div class="menu-login-signup">
188 <?php
189 if (isset($_SESSION['email']) && isset($_SESSION['token'])) {
190 if ($email == $_server_email && $token == $_server_token)
191 {
192 echo '<a href="includes/logout.php" class="user-logout">Logout</a>';
193 }
194 } else {
195 echo '<a href="#" class="login">Login</a>
196 <a href="#" class="signup">Signup</a>';
197 }
198 ?>
199 </div>
200 <div class="menu-cart">
201 <div class="cart-count">
202 <p>
203 <?php echo $totalCartProducts; ?>
204 </p>
205 </div>
206 <p>Cart</p>
207 </div>
208 </div>
209 </nav>
210
211 <div class="products-container">
212 <div class="cart-wrapper">
213 <h2>Cart <?php echo '(' . $totalCartProducts . ')'; ?></h2>
214 <div class="cart-products">
215 <?php
216 $cartProductsCount = $allCartProducts -> rowCount();
217
218 for ($i=0; $i < $cartProductsCount; $i++) {
219 $cartProductQuantitySelected = '';
220
221 for ($j=1; $j <= 4; $j++) {
222 # Loop through all the quantity of the cart product
223 if ($allCartProductsRow[$i]['product_quantity'] == $j) {
224 # Print normal select option
225 $cartProductQuantitySelected .= '
226 <option selected="selected" value="' . $j . '">' . $j . '</option>
227 ';
228 } else {
229 # Print normal select option
230 $cartProductQuantitySelected .= '
231 <option value="' . $j . '">' . $j . '</option>
232 ';
233 }
234 }
235
236 # Display the products
237 echo '
238 <div class="product">
239 <div class="product-image-wrapper">
240 <img src="' . $allCartProductsRow[$i]['product_image'] . '" alt="' . $allCartProductsRow[$i]['product_name'] . '">
241 </div>
242 <div class="product-content-wrapper">
243 <div class="content">
244 <h4>' . $allCartProductsRow[$i]['product_name'] . '</h4>
245 <p>$' . $allCartProductsRow[$i]['product_price'] . '</p>
246 <select name="' . $allCartProductsRow[$i]['product_name'] . '" class="number_of_products">
247 ' . $cartProductQuantitySelected . '
248 </select>
249 <img src="images/icons/icon_close.png" alt="' . $allCartProductsRow[$i]['product_name'] . '" class="remove_product">
250 </div>
251 </div>
252 <div class="clearfix"></div>
253 </div>
254 ';
255 }
256 ?>
257 </div>
258 </div>
259 <div class="checkout-wrapper">
260 <p>Total Items: <span><?php echo $totalProducts; ?></span></p>
261 <p>Products Price:
262 <span>
263 <?php
264 $totalPrice = number_format((float)$totalPrice, 2, '.', '');
265 echo '$'.$totalPrice;
266 ?>
267 </span>
268 </p>
269 <p>GST (18%):
270 <span>
271 <?php
272 $gst = $totalPrice * 18 / 100;
273 $gst = number_format((float)$gst, 2, '.', '');
274 echo '$'.$gst;
275 ?>
276 </span>
277 </p>
278 <p>Rounded Price:
279 <span>
280 <?php
281 $priceDiff = $totalPrice + $gst - round($totalPrice + $gst);
282
283 $priceDiff = number_format((float)$priceDiff, 2, '.', '');
284 echo '$'.$priceDiff;
285 ?>
286 </span>
287 </p>
288 <div class="spacer"></div>
289 <p>Total Price: </p>
290 <p class="total-price">
291 <span>
292 <?php
293 $totalPriceAfterGST = $totalPrice + $gst - $priceDiff;
294
295 $totalPriceAfterGST = number_format((float)$totalPriceAfterGST, 2, '.', '');
296 echo '<sup>$ </sup>'.$totalPriceAfterGST;
297 ?>
298 </span>
299 </p>
300 <?php
301 if ($totalCartProducts > 0) {
302 # Show Checkout button only if user cart has product
303 echo '
304 <a href="shipping.php" class="checkout-button"> Checkout
305 </a>
306 ';
307 }
308 ?>
309 </div>
310 </div>
311 </div>
312
313 <!-- jQuery CDN -->
314 <script
315 src="https://code.jquery.com/jquery-3.3.1.min.js"
316 integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
317 crossorigin="anonymous"></script>
318
319 <!-- Custom JS -->
320 <script src="js/main.js"></script>
321 </body>
322 </html>